package cn.itqiqi.qcloudsonsumer.interceptor;

import cn.hutool.json.JSONUtil;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.TreeMap;

public class AuthInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        String token = request.getHeader("token");

        if (token != null && !token.equals("")) {
            return true;
        } else {
            String jsonStr = JSONUtil.toJsonStr(new TreeMap<Object, Object>() {
                {
                    put("code", 1186);
                    put("msg", "请登录！");
                    put("success", false);
                }
            });
            response.setContentType("application/json;charset=utf-8");
            response.getWriter().print(jsonStr);
            return false;
        }
    }
}
